package com.tamako.maaremote.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器注册
 *
 * @author Tamako
 */
@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginAuthInterceptor loginAuthInterceptor = new LoginAuthInterceptor();
        registry.addInterceptor(loginAuthInterceptor)
                .addPathPatterns("/**");
    }

    // cors跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 添加路径规则
        registry.addMapping("/**")
                // 是否允许在跨域的情况下传递Cookie
                .allowCredentials(true)
                // 允许请求来源的域规则
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                // 允许所有的请求头
                .allowedHeaders("*");
    }

}